package com.zmn.oms.task.listener.clue;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.entity.order.OrderExtend;
import com.zmn.oms.services.interfaces.order.OrderExtendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author sunlife
 * @date:2022/7/20 下午6:35
 * description:
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class ClueCallBackListener {

    @Autowired
    OrderExtendService orderExtendService;

    @AliyunMQHandler(tags = OmsMqTagConsts.CLUE_CALL_RELATION_TAG)
    public void onChange(String message) {
        log.info("[线索ID回传]信息：{}", message);
        try {
            JSONObject jb = JSON.parseObject(message);
            Long clueId = jb.getLong("clueId");
            String callId = jb.getString("callId");
            Long orderId = jb.getLong("orderId");
            if (NumberUtil.isNullOrZero(orderId)) {
                return;
            }

            OrderExtend orderExtend = new OrderExtend();
            orderExtend.setExtId(orderId);
            orderExtend.setBizClueId(clueId);
            orderExtend.setUpdater("系统");
            orderExtendService.updateBizClueId(orderExtend);

        } catch (Exception e) {
            log.error("线索ID回传消费失败", e);
        }
    }

}
